Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
This last characteristic of the DirListBox is the one we’ll use. It enables us to obtain the names of the current folder’s subfolders. Apply a recursive algorithm as follows: 1. Start in the directory specified by the user. 2. Use the Dir function to search the current directory for files that match the template. If any files are found, add them to the list of matching files. 3. Check for any subfolders in this folder. If none exists, you are finished with this folder and are ready to move up one level to the parent folder. If you have returned to the starting folder, you are finished. If you have subfolders, use the List and ListCount properties to obtain the subfolder names and create a list of them. 4. Make the first folder in this list current. 5. Return to Step 2. This general type of algorithm is called recursion. The same procedures are applied over and over to the data until no more data remains to process. In this case, the data consists of folder names. As you’ll see in the demonstration program, the code required to implement the file search is actually quite simple. As is often the case in computer programming, having the proper algorithm is an important step in designing code. #### Creating The Form One important Visual Basic technique illustrated by this project is the use of hidden controls. Just because a control is hidden (Visible property = False) does not mean it is nonfunctional. Of course, the user cannot interact with it, but it can be used by code. In this project, you will use a hidden DirListBox control to implement the file-search algorithm. Start a new Standard EXE project. Add a DriveListBox and two DirListBox controls, setting the Visible property of one of the DirListBoxes to False. Add a Check Box, a List Box, and a control array of three Command Buttons. Finally, add one Text Box and three labels to identify the Text Box, the visible DirListBox, and the DriveList Box. The form should look more or less like Figure 14.4. The DirListBox superimposed on the ListBox is the one that will be hidden. Set control and form properties as shown in Listing 14.5. Please note the new format in Listing 14.5. This is the format used by Visual Basic within a form file to record the form’s controls and their properties. If you open a FRM file in a text editor, you’ll see the controls and properties listed at the top of the file and the Basic code listed at the end. Starting with this project, I will sometimes be presenting controls and properties in this more efficient format rather than detailing them individually in the text. While the original FRM file lists all of the properties for each control, I will include only those that you need to change from their default values. By looking through a control/property listing, such as Listing 14.5, you can determine the various property settings you need to make. Listing 14.5 Objects and properties in FINDFILE.FRM.
    Begin VB.Form Form1
        Appearance       =   0  ‘3D
        BorderStyle      =   1  ‘Fixed Single
        Caption          =   “File Finding Utility”
        Begin VB.ListBox lstFiles
            BeginProperty Font
                name     =   “Courier New”
                size     =   9.75
            EndProperty
        End
        Begin VB.CheckBox chkFileInfo
            Caption      =   “Include file information in list”
        End
        Begin VB.DirListBox dirHidden
        Visible          =   False
        End
        Begin VB.DirListBox dirStart
        End
        Begin VB.DriveListBox Drive1
            Appearance   =   1  ‘3D
        End
        Begin VB.CommandButton Command1
            Caption      =   “&Quit”
            Index        =   2
        End
        Begin VB.CommandButton Command1
            Appearance   =   0  ‘Flat
            Caption      =   “Sto&p”
            Index        =   1
        End
        Begin VB.CommandButton Command1
            Appearance   =   0  ‘Flat
            Caption      =   “&Start”
            Index        =   0
        End
        Begin VB.TextBox txtPattern
            Text         =   “Text1”
        End
        Begin VB.Label Label3
            Alignment    =   1  ‘Right Justify
            Caption      =   “File template:”
        End
        Begin VB.Label Label2
            Alignment    =   1  ‘Right Justify
            Caption      =   “Starting directory:”
        End
        Begin VB.Label Label1
            Alignment    =   1  ‘Right Justify
            Caption      =   “Drive:”
        End
    End
#### Writing The Code The program requires two global variables. The first is a type Boolean flag that signals whether the user has canceled the search; the other is a type Integer that keeps track of the depth of the search—in other words, how many folders “deep” execution is. These variables should be declared in the general declarations section of code, shown in Listing 14.6. Figure 14.4 The File Finding utility’s form. Listing 14.6 General declarations in FINDFILE.FRM.
    Option Explicit

    ‘ Global flag to abort search.

    Dim Halt As Boolean
    Dim Depth As Integer
The program actions are triggered by the Click event procedure for the Command Button control array. It’s quite simple, as you can see in Listing 14.7. To start the search, it calls the procedure StartSearch. To terminate an ongoing search, it sets the flag variable Halt to True. To exit the program, it executes the End statement. Listing 14.7 The Command Button Click event procedure.
    Private Sub Command1_Click(Index As Integer)

    Select Case Index
        Case 0 ‘Start button
            Call StartSearch
        Case 1 ‘Stop button
            Halt = True
        Case 2      ‘ Exit button
            End
    End Select

    End Sub
The StartSearch procedure is where the action begins. This is a general procedure that you add to the module with the Insert Procedure command. Its code is shown in Listing 14.8. First, code in this procedure clears the List Box where the search results will be displayed and sets the Halt flag to False. It then disables all of the controls that should not be available during a search. Next, turn your attention to the hidden DirListBox control. It is this control’s Change event procedure that triggers the actual search action. You need to set this control’s Path property to the folder that the user specified as the start folder for the search, obtained from the Path property of the visible DirListBox. This property change will normally trigger the control’s Change event. But what if the hidden DirListBox control’s Path property is already pointing to the desired start directory? Because no Change event will be generated, we’ll have to generate one ourselves (remember that event procedures can be called in code just like nonevent procedures). Here is the code:
    If dirHidden.Path = dirStart.Path Then
        dirHidden_Change
    Else
        dirHidden.Path = dirStart.Path
    End If
Note that the program displays the hourglass mouse pointer while the search is in progress. Once that Change event is triggered for the hidden DirListBox control, execution recurses within event procedures until the search is completed. Then it returns to the same location in the StartSearch procedure. The final lines of code in this procedure reenable the form’s controls that were disabled when the search began, returning the normal mouse pointer.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.